package com.cetc.baidumaptest;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.CircleOptions;
import com.baidu.mapapi.map.DotOptions;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.Stroke;
import com.baidu.mapapi.model.LatLng;

/**
 * Created by Young on 2017/1/12.
 */

public class CircleOutActivity extends AppCompatActivity {
    private static final double EARTH_RADIUS = 6378.137;//地球半径,单位千米
    MapView mMapView = null;
    BaiduMap mBaiduMap = null;
    TextView txt_center = null;
    LatLng center = new LatLng(39.90923, 116.447428);
    LatLng position = null;
    EditText edit_radius = null;
    Button btn_draw = null;
    Boolean OUT = true;
    public int radius;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //在使用SDK各组件之前初始化context信息，传入ApplicationContext
        //注意该方法要再setContentView方法之前实现
        SDKInitializer.initialize(getApplicationContext());
        setContentView(R.layout.activity_circle);
        txt_center = (TextView) findViewById(R.id.txt_center);
        edit_radius = (EditText) findViewById(R.id.edit_radius);
        edit_radius.setHint("请在地图选取一点后进行判断");
        edit_radius.setCursorVisible(false);
        edit_radius.setClickable(false);
        btn_draw = (Button) findViewById(R.id.btn_draw_circle);
        btn_draw.setHint("判断");
        mMapView = (MapView) findViewById(R.id.mapView);
        mBaiduMap = mMapView.getMap();
        OverlayOptions ooCircle = new CircleOptions().fillColor(0x660000FF)
                .center(center).stroke(new Stroke(10, 0xFF0000FF))
                .radius(5000);
        mBaiduMap.addOverlay(ooCircle);
        mBaiduMap.setOnMapClickListener(new BaiduMap.OnMapClickListener() {
            public void onMapClick(LatLng point) {
                //在此处理点击事件
                position = point;
                txt_center.setText("纬度" + point.latitude + " 经度" + point.longitude);
                OverlayOptions ooDot = new DotOptions().center(position).radius(10)
                        .color(0xFFFF3030);
                mBaiduMap.addOverlay(ooDot);
            }

            public boolean onMapPoiClick(MapPoi poi) {
                //在此处理底图标注点击事件
                return false;
            }
        });
        btn_draw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//判断是否在圆内
                if (getDistance(39.90923, 116.447428, position.latitude, position.longitude) < 5000) {
                    Toast.makeText(getApplicationContext(), "内部", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(getApplicationContext(), "外部", Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    public static double getDistance(double lat1, double lng1, double lat2, double lng2) {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lng1) - rad(lng2);

        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
                Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000;
        return 1000 * s;

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        mMapView.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mMapView.onPause();
    }
}
